package com.kungfuhacking.wristbandpro.location.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.kungfuhacking.wristbandpro.R;
import com.kungfuhacking.wristbandpro.base.bean.Basebean;
import com.kungfuhacking.wristbandpro.base.bean.RequestBean;
import com.kungfuhacking.wristbandpro.base.view.BaseActivity;
import com.kungfuhacking.wristbandpro.login.activity.LoginActivity;
import com.kungfuhacking.wristbandpro.net.BaseNoTObserver;
import com.kungfuhacking.wristbandpro.net.RetrofitHttpUtil;
import com.kungfuhacking.wristbandpro.utils.Constant;

import io.reactivex.disposables.Disposable;

/**
 * 设置手表电话页面
 * Created by Aaron on 17/4/26.
 */

public class ModifyWritbandTelActivity extends BaseActivity implements View.OnClickListener {

    private String id;
    private EditText et_phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_modify_phone);

        initWidget();
        id = getIntent().getStringExtra("id");

    }

    private void initWidget() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_right = (TextView) findViewById(R.id.tv_right);
        et_phone = (EditText) findViewById(R.id.et_phone);

        iv_left.setOnClickListener(this);
        tv_right.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.iv_left:
                finish();
                break;

            case R.id.tv_right:

                if (TextUtils.isEmpty(et_phone.getText().toString())
                        || et_phone.getText().toString().length() != 11) {
                    showErr("请输入正确的手机号码");
                    return;
                }

                updateTel();
                break;


        }
    }

    private Disposable disposable;

    private void updateTel() {

        JSONObject j = new JSONObject();
        j.put("token", sp.getString(Constant.TOKEN, ""));
        j.put("id", id);
        j.put("phoneNum", et_phone.getText().toString());

        RetrofitHttpUtil.getInstance().modifyWatchPhone(new BaseNoTObserver<Basebean>() {
            @Override
            public void onHandleSuccess(Basebean basebean) {
                hideLoading();
                showErr("设置成功");
                Intent intent = new Intent();
                intent.putExtra("tel", et_phone.getText().toString());
                setResult(10034, intent);
                finish();
            }

            @Override
            public void onHandleError(String message) {
                showErr(message);
                hideLoading();
            }

            @Override
            public void onReLogin(String message) {
                showErr(message);
                hideLoading();
                nextActivityWithSingleTask(LoginActivity.class);
            }

            @Override
            public void onSubscribe(Disposable d) {
                disposable = d;
                showLoading("设置中...");
            }
        }, new RequestBean().setData(j.toString()));

    }
}
